package com.resource.controller;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.resource.constant.OfficeApiConstants;
import com.resource.enums.ProcessFormEnum;
import com.resource.service.OfficeProcessFormService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 流程表单业务处理
 *
 * @Author: xianggaoxiong
 * @Date: 2020-11-12 14:34
 **/
@Slf4j
@RestController
@RequestMapping(value = "/api/process/formData")
public class OfficeProcessFormController {
    @Resource
    private OfficeProcessFormService processFormService;

    /**
     * 差旅报销-联动出差信息
     * @param request
     * @return
     */
    @RequestMapping("/getTravelInfos")
    public List<JSONObject> getTravelInfos(HttpServletRequest request){
        String[] runIds = request.getParameterMap().get("DATA_1131");
        if(ArrayUtil.isNotEmpty(runIds) && StrUtil.isNotEmpty(runIds[0])){
            return processFormService.getTravelInfos(ProcessFormEnum.TRAVEL.getType(),runIds[0]);
        }
        return new ArrayList<>();
    }

    /**
     * 接待报销-联动接待单据申请人
     * @param request
     * @return
     */
    @RequestMapping("/getReceiver")
    public JSONObject getReceiver(HttpServletRequest request){
        String[] runIds = request.getParameterMap().get("DATA_1131");
        String[] runNames = request.getParameterMap().get("DATA_1131_TEXT");
        if(ArrayUtil.isNotEmpty(runIds) && StrUtil.isNotEmpty(runIds[0])){
            ProcessFormEnum formType=null;
            //判断接待单据类型：市场接待或公司接待
            if(runNames[0].contains(OfficeApiConstants.MARKET_RECEPTION)){
                formType=ProcessFormEnum.MARKET_RECEPTION;
            }else{
                formType=ProcessFormEnum.COMPANY_RECEPTION;
            }
            return processFormService.getReceiver(formType,runIds[0]);
        }
        return new JSONObject();
    }
}
